package com.dingding.order.core.history.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.order.code.order.PublishCarType;
import com.dingding.order.core.history.dao.IHistoryOrderDao;
import com.dingding.order.core.history.dto.TakeMileageDTO;
import com.dingding.order.core.history.service.IHistoryOrderService;
import com.dingding.order.dto.history.HistoryOrderDto;
import com.dingding.order.dto.history.ReqHistoryOrder;
import com.dingding.order.dto.history.ResHistoryOrder;
import com.dingding.order.processer.ProcesserFactory;

@Component("customerOrderService")
public class CustomerOrderServiceImpl extends AbstractResponse<ResHistoryOrder,ReqHistoryOrder> implements IHistoryOrderService{

	@Resource(name = "processerFactory")
	private ProcesserFactory processerFactory;
	
	@Resource(name = "specialHistoryOrderDao")
	private IHistoryOrderDao historyOrderDao;
	
	@Override
	public String getHistoryOrders(String clientData) {

		ResponseEntity<ResHistoryOrder> responseEntity = new ResponseEntity<ResHistoryOrder>();
		ReqHistoryOrder reqHistoryOrder = super.parseRequestJson(clientData);
		
		if(!super.validateParam(responseEntity, reqHistoryOrder)){
			return super.responseBeanToJson(responseEntity);
		}
		
		List<HistoryOrderDto> list = new ArrayList<HistoryOrderDto>();
		for(PublishCarType publishCarType : PublishCarType.values()){
			List<HistoryOrderDto> historyOrderDtos = processerFactory.getOrderProcesser(publishCarType)
					.getHistoryOrders(
							reqHistoryOrder.getUserId(), reqHistoryOrder.getOrderStatus(), 
							reqHistoryOrder.getPageIndex(), reqHistoryOrder.getPageSize(),reqHistoryOrder.getClientType());
			for(HistoryOrderDto dto : historyOrderDtos){
				list.add(dto);
			}
		}
		
		// 获取乘客的接乘里程和搭乘里程(专车)
		TakeMileageDTO takeMileageDTO = historyOrderDao.takeMileage(reqHistoryOrder.getUserId());
		
		// 获取乘客的接乘里程和搭乘里程
		TakeMileageDTO mileage = historyOrderDao.getMileages(reqHistoryOrder.getUserId());
		
		//对list中所有条目进行按创建时间倒序排序
		Collections.sort(list, new Comparator<HistoryOrderDto>() {
			@Override
			public int compare(HistoryOrderDto o1, HistoryOrderDto o2) {
				return o2.getSetOutTime().compareTo(o1.getSetOutTime());
			}
		});		
		
		ResHistoryOrder resHistoryOrder = new ResHistoryOrder();
		resHistoryOrder.setOrderList(list);
		
		resHistoryOrder.setPickUpMeters(new BigDecimal(0));
		resHistoryOrder.setRideMeters(new BigDecimal(0));
		
		// 获取乘客的接乘里程和搭乘里程
		if(mileage != null){
			
			// 如果搭乘里程不为空，则赋值
			if(mileage.getTotalWeldingProcess() != null){
				resHistoryOrder.setRideMeters(mileage.getTotalWeldingProcess());
			}
			
			// 如果接乘里程不为空，则赋值
			if(mileage.getTotalTake() != null){
				resHistoryOrder.setPickUpMeters(mileage.getTotalTake());
			}
			
		}
		
		// 获取乘客的接乘里程和搭乘里程(专车)
		if(takeMileageDTO != null){
			
			if(takeMileageDTO.getPickUpMeters() != null){
				resHistoryOrder.setPickUpMeters(resHistoryOrder.getPickUpMeters().add(mileage.getTotalWeldingProcess()));
			}
			
		}
		
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setPromptMessage("请求成功");
		responseEntity.setResponseBody(resHistoryOrder);
		return super.responseBeanToJson(responseEntity);
	}

	@Override
	public String getHistoryOrdersByCarType(String clientData, PublishCarType carType) {
		return null;
	}
}
